//
//  EmoticonTextAttachment.swift
//  EmoticonKeyboard
//
//  Created by liii on 16/8/20.
//  Copyright © 2016年 liii. All rights reserved.
//

import UIKit

class EmoticonTextAttachment: NSTextAttachment {

    var chs: String?
    
    // 用图片创建一个, 图片属性文本
    class func imageAttributedStringWithEmotion(em: Emoticon, font: UIFont) -> NSAttributedString {
    
        // 5.1 把表情图片添加到, 文本附件
        let attachment = EmoticonTextAttachment()
        attachment.image = UIImage(contentsOfFile: em.imgPath ?? "")
        attachment.chs = em.chs // 获取表情图片的字符串
        
        // 5.6 设置表情的大小
        let lineHeight = font.lineHeight ?? 0
        attachment.bounds = CGRect(x: 0, y: -4, width: lineHeight, height: lineHeight)
        
        // 5.2 把图片转换成"属性文本"
        let emoticonImageText = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attachment))
        
        // 5.9 设置第一个按钮的大小
        emoticonImageText.addAttribute(NSFontAttributeName, value: font, range: NSRange(location: 0, length: 1))
        
        return emoticonImageText
    }
    
}
